﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Barbar.UrlGuard.BL;
using System.Collections.ObjectModel;

namespace Barbar.UrlGuard.Dal {
  public class EmailAlertSettings {
    private string m_SmtpServer;
    private string m_UserName;
    private string m_Password;
    private bool m_AuthenticateSmtp;
    private bool m_UseSsl;
    private int m_Port = 25;
    private List<string> m_Emails = new List<string>();

    public bool AuthenticateSmtp {
      get { return m_AuthenticateSmtp; }
      set { m_AuthenticateSmtp = value; }
    }

    public bool UseSsl {
      get { return m_UseSsl; }
      set { m_UseSsl = value; }
    }

    public int Port {
      get { return m_Port; }
      set { m_Port = value; }
    }

    public string SmtpServer {
      get { return m_SmtpServer; }
      set { m_SmtpServer = value; }
    }

    public string UserName {
      get { return m_UserName; }
      set { m_UserName = value; }
    }

    public string EncryptedPassword {
      get {
        ISymmetricPasswordService passwordService = ServiceLocator.Instance.Resolve<ISymmetricPasswordService>();
        return passwordService.EncryptPassword(m_Password);
      }
      set {
        ISymmetricPasswordService passwordService = ServiceLocator.Instance.Resolve<ISymmetricPasswordService>();
        m_Password = passwordService.DecryptPassword(value); 
      }
    }

    public List<string> Emails {
      get { return m_Emails; }
    }

    [XmlIgnore()]
    public string EmailsAsText {
      get { return string.Join(",", m_Emails.ToArray()); }
      set {
        m_Emails.Clear();
        if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim())) {
          return;
        }

        string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string part in parts) {
          m_Emails.Add(part.Trim());
        }
      }
    }

    [XmlIgnore()]
    public string Password {
      get { return m_Password; }
      set { m_Password = value; }
    }
  }
}
